﻿using ConfExpert.Forms.Base;
using DevExpress.XtraCharts;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class DepoGirisCikis : XForm
    {
        public DepoGirisCikis()
        {
            InitializeComponent();
        }

        private void DepoGirisCikis_Load(object sender, EventArgs e)
        {
            //chartControl1.PaletteName = "Verde";
            
            chartControl1.CustomDrawSeriesPoint += chartControl1_CustomDrawSeriesPoint;
            chartControl1.CustomDrawAxisLabel += chartControl1_CustomDrawAxisLabel;

            dateEdit1.EditValueChanged += dateEdit1_EditValueChanged;
            dateEdit1.EditValue = new DateTime(2013, 1, 1);
        }


        private void chartControl1_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
            var pointValue = e.SeriesPoint.Values[0];
            e.LabelText = e.SeriesPoint.Argument + " : " + pointValue;
        }


        void dateEdit1_EditValueChanged(object sender, System.EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            d = ExecuteDataSet("GetDepoGelisCikis", dateEdit1.EditValue);
            BindDataToChart();
        }

        DataSet d;
        string[] val = "gQty;cQty".Split(";".ToCharArray());
        private void BindDataToChart()
        {

            DataTable dt;
            int seri = 0;
            dt = d.Tables["Table"];

            if (!chartControl1.Series.Contains(chartControl1.Series["gQty"])) 
                chartControl1.Series.Add("gQty", ViewType.Bar);

            if (!chartControl1.Series.Contains(chartControl1.Series["cQty"]))
                chartControl1.Series.Add("cQty", ViewType.Bar);

            //if (!chartControl1.Series.Contains(chartControl1.Series["tQty"]))
            //    chartControl1.Series.Add("tQty", ViewType.Bar);

            foreach (string str in val)
            {
                chartControl1.Series[seri].DataSource = dt;
                chartControl1.Series[seri].ValueDataMembers.Clear();

                chartControl1.Series[seri].ValueDataMembers.AddRange(str);

                chartControl1.Series[seri].ArgumentDataMember = "Tarih";
                seri++;
            }

            ((XYDiagram)chartControl1.Diagram).AxisX.Tickmarks.CrossAxis = true;
            ((XYDiagram)chartControl1.Diagram).AxisX.DateTimeScaleOptions.AutoGrid = true;  //GridSpacingAuto 
        }

        void chartControl1_BoundDataChanged(object sender, EventArgs e)
        {
            SetDateFormat();
        }

        void chartControl1_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {

            if (e.Item.AxisValue.GetType() == typeof(DateTime))
                e.Item.Text = ((DateTime)e.Item.AxisValue).ToString("dd.MM.yyyy");
            else if (e.Item.AxisValue.GetType() == typeof(double))
                e.Item.Text = ((double)e.Item.AxisValue).ToString("#,###");
        }

        private void SetDateFormat()
        {
            return;
            //(chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day;
            //(chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
            //(chartControl1.Diagram as XYDiagram).AxisX.Label.DateTimeOptions.Format = DateTimeFormat.ShortDate;
        }

    }
}
